package com.phy.gsjl.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.phy.gsjl.dao.MessageDAO;
import com.phy.gsjl.entity.Message;
import com.phy.gsjl.form.QueryForm;
import com.phy.gsjl.service.IMessageService;

/**
 * message服务层
 * @author curry
 *
 */
@Transactional
@Service
public class MessageServiceImpl implements IMessageService {

	@Autowired
	public MessageDAO messageDAO;

	//获取日志对象
	private static Logger logger = Logger.getLogger(MessageServiceImpl.class);
	
	@Override
	public List<Message> getAllMessages(Integer type) {
		logger.info("==>In MessageServiceImpl getAllMessages parameter type: " + type);
		return messageDAO.getAllMessages(type); //调用DAO层的查询方法，返回message的list集合
	}

	@Override
	public Integer getTotalRecords(Integer type) {
		Integer records = messageDAO.getTotalRecords(type); //调用DAO层的查询方法，返回总记录数
		logger.info("==>In MessageServiceImpl getTotalRecords parameter type: " + type);
		logger.info("==>In MessageServiceImpl getTotalRecords return : " + records);
		return records;
	}

	@Override
	public Message getMessageById(Integer m_id) {
		Message message = messageDAO.getMessageById(m_id); //调用DAO层的查询方法，返回message实体
		logger.info("==>In MessageServiceImpl getMessageById parameter m_id: " + m_id);
		logger.info("==>In MessageServiceImpl getMessageById return message: " + message.toString());
		return message;
	}

	@Override
	public List<Message> getMessageIndistinct(Integer type, String indistinct) {
		logger.info("==>In MessageServiceImpl getMessageIndistinct parameter type: " + type + "indistinct: " + indistinct);
		//调用DAO层的查询方法，返回message的list集合
		return messageDAO.getMessageIndistinct(type, indistinct); 
	}

	@Override
	public List<Message> getMessageByDetailsAndTime(QueryForm queryForm) {
		logger.info("==>In MessageServiceImpl getMessageByDetailsAndTime parameter queryForm: " + queryForm.toString());
		//调用DAO层的查询方法，返回message的list集合
		return messageDAO.getMessageByDetailsAndTime(queryForm); 
	}
	
	@Override
	public boolean addMessage(Message message) {
		try {
			message.setM_number(numberHelper(message.getType())); //生成序号
			logger.info("==>In MessageServiceImpl addMessage message number:" + message.getM_number());
			messageDAO.addMessage(message); //调用DAO层的插入方法
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}		
	}

	@Override
	public boolean updateMessageStatus(Message message) {
		try {
			logger.info("==>In MessageServiceImp updateMessageStatus message status(before):" + message.getStatus());
			messageDAO.updateMessageStatus(message); //调用DAO层的更新方法
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	@Override
	public List<Message> getMessageByPage(Integer currentPage, Integer type) {
		Integer totalRecord = messageDAO.getTotalRecords(type);
		//调用DAO层的查询方法，返回message的list集合
		List<Message> messages = messageDAO.getCurrentPage(currentPage, totalRecord, type);
		return messages;
	}
	
	/**
	 * 生成序号
	 * @param type
	 * @return
	 */
	public String numberHelper(Integer type) {
		Date date = new Date(); 
		int k = 1; //定义流水号
		String maxNumber = messageDAO.getMessageMaxNum(type); //获取记录中最大的序号
		if (maxNumber != null) {
			String maxDate = maxNumber.substring(0,8); //截取日期
			int maxId = Integer.parseInt(maxNumber.substring(8)); //截取流水号
			String currentdate = String.format("%tY%<tm%<td", date); //安照相同的格式，格式化当前日期
			if (currentdate.equals(maxDate)) {
				k = maxId + 1; //每增加一条消息就加一
			}
		}
        String number = String.format("%tY%<tm%<td%03d", date, k); //生成序号
		return number;
	}
	
}
